package com.example.mine.shiro;

import com.example.mine.mapper.MenusMapper;
import com.example.mine.pojo.Menus;
import org.apache.commons.lang.StringUtils;
import com.example.mine.mapper.UserMapper;
import com.example.mine.pojo.User;
import com.example.mine.pojo.UserExample;
import com.example.mine.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class CustomRealm extends AuthorizingRealm{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenusMapper menusMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User) principalCollection.getPrimaryPrincipal();
        Long roleId = user.getRoleId();

        List<String> permsList = null;
        // 系统管理员，拥有最高权限
        List<Menus> menuList = menusMapper.getMenus(roleId);
        permsList = new ArrayList<>(menuList.size());
        for (Menus menu : menuList) {
            if (menu.getPerms() != null && !"".equals(menu.getPerms())) {
                permsList.add(menu.getPerms());
            }
        }
        // 用户权限列表
        Set<String> permsSet = new HashSet<String>();
        for (String perms : permsList) {
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("-------身份认证方法--------");
        String userName = (String) authenticationToken.getPrincipal();
        String userPwd = new String((char[]) authenticationToken.getCredentials());
        //根据用户名从数据库获取密码

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andNickNameEqualTo(userName);
        List<User> users = userMapper.selectByExample(example);
        String password = users.get(0).getPassword();
        // 账号不存在
        if (users == null || users.size() == 0) {
            throw new UnknownAccountException("账号不存在!");
        }
        // 密码错误
        if (!password.equals(users.get(0).getPassword())) {
            throw new IncorrectCredentialsException("账号或密码不正确!");
        }
        // 账号未分配角色
        if (users.get(0).getRoleId() == null || users.get(0).getRoleId() == 0) {
            throw new UnknownAccountException("账号未分配角色!");
        }
        return new SimpleAuthenticationInfo(users.get(0), password,getName());
    }
}
